﻿using System.Timers;
using Gfw.Models.Stock;
using Gfw.Repositories.Stock;

namespace StockService.Manager
{
    public static class MarketManager
    {
        public static Timer upTimer;
        public static void  Start()
        {
            if (upTimer == null)
            {
                upTimer = new Timer()
                              {
                                  AutoReset = true,
                                  Enabled = false,
                                  Interval = 1000*60
                              };

                upTimer.Elapsed += new ElapsedEventHandler(upTimer_Elapsed);
            }
        }
        public static void Stop()
        {
            if (upTimer.Enabled)
            {
                upTimer.Stop();
                upTimer.Close();
            }
            upTimer = null;
        }

        /// <summary>
        /// 开始计时
        /// </summary>
        public static void UpPriceStart()
        {
            Start();
            upTimer.Start();
        }

        public static void UpPriceStop()
        {
            Start();
            upTimer.Stop();
        }

        static void upTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _UpPrice();
        }

        
        #region 价格维护

        private const double UpUnit = 0.001;
        private static MarketRepository marketRepository = new MarketRepository();
        private static StkMarket market;

        public static StkMarket Market
        {
            get { return market ?? (market = marketRepository.Unique()); }
        }

        private static void _UpPrice()
        {
            Market.CurrentPrice += UpUnit;
            marketRepository.Update(market);
        }

        #endregion
    }
}
